package com.le.tester.leetcode;


/**
 * 描述：给出一个 32 位的有符号整数，将这个整数中每位上的数字进行反转。
 */
public class IntegerInversion {
    public int reverse(int x) {
        //代表符号位
        int sign = 1;
        String changeX = String.valueOf(x);
        if (changeX.charAt(0) == '-') {
            changeX = changeX.substring(1);
            sign = -1;
        }
        int length = changeX.length();
        char[] resultChars = new char[length];

        for (int i = length - 1; i >= 0; i--) {
            resultChars[length - 1 - i] = changeX.charAt(i);
        }
        long resultNum = Long.parseLong(new String(resultChars));
        if (resultNum > Integer.MAX_VALUE || resultNum < Integer.MIN_VALUE) {
            return 0;
        }
        return (int) resultNum * sign;
    }

    public static void main(String[] args) {
        IntegerInversion integerInversion = new IntegerInversion();
        System.out.println(integerInversion.reverse(-123));
    }
}
